package cn.itsource.pet.controller;

import cn.itsource.basic.util.CurrentUserUtil;
import cn.itsource.basic.util.PageBean;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.service.IEmployeeService;
import cn.itsource.pet.domain.dto.AcceptSearchMasterMsgDto;
import cn.itsource.pet.service.ISearchMasterMsgService;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.query.SearchMasterMsgQuery;
import cn.itsource.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;
    @Autowired
    private IEmployeeService employeeService;


    /**
     * 保存和修改公用的
     * @param searchMasterMsg  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg){
        if( searchMasterMsg.getId()!=null)
            searchMasterMsgService.update(searchMasterMsg);
        else
            searchMasterMsgService.add(searchMasterMsg);
        return AjaxResult.me();
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        searchMasterMsgService.delete(id);
        return AjaxResult.me();
    }
	
    //获取用户
    @GetMapping("/{id}")
    public SearchMasterMsg get(@PathVariable("id")Long id)
    {
        return searchMasterMsgService.queryById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<SearchMasterMsg> list(){
        return searchMasterMsgService.queryAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageBean<SearchMasterMsg> json(@RequestBody SearchMasterMsgQuery query)
    {
        return searchMasterMsgService.pageList(query);
    }

    /**
     * 寻主消息发布
     * @param msg
     * @return
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg msg , HttpServletRequest request) throws Exception {
        Long id = CurrentUserUtil.getCurrentUtil(request);
        if(id != null){
            msg.setUserId(id);
        }
        return searchMasterMsgService.publish(msg,request);
    }



    //我发布的
    @PostMapping("/my_publish")
    public PageBean<SearchMasterMsg> myPublish(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request) throws Exception {
        Long userId = CurrentUserUtil.getCurrentUtil(request);
        query.setUserId(userId);
        return searchMasterMsgService.pageList(query);
    }
    //驳回的
    @PostMapping("/reject")
    public PageBean<SearchMasterMsg> reject(@RequestBody SearchMasterMsgQuery query){
        query.setState(-1);
        return searchMasterMsgService.pageList(query);
    }
    //寻主池
    @PostMapping("/pool")
    public PageBean<SearchMasterMsg> pool(@RequestBody SearchMasterMsgQuery query){
        query.setState(1);
        return searchMasterMsgService.pageList(query);
    }
    //店铺待处理的
    @PostMapping("/to_handle")
    public PageBean<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request) throws Exception{
        Long empId = CurrentUserUtil.getCurrentUtil(request);
        Employee employee = employeeService.queryById(empId);
        query.setShopId(employee.getShop_id());
        query.setState(1);
        return searchMasterMsgService.pageList(query);
    }

    //已完成的
    @PostMapping("/finish")
    public PageBean<SearchMasterMsg> finish(@RequestBody SearchMasterMsgQuery query){
        query.setState(2);
        return searchMasterMsgService.pageList(query);
    }


    /**
     * 店铺拒单
     * @param id
     * @return
     */
    @GetMapping("/reject/{id}")
    public AjaxResult reject(@PathVariable Long id){
        return searchMasterMsgService.reject(id);
    }

    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody AcceptSearchMasterMsgDto dto){
        return searchMasterMsgService.accept(dto);
    }
}
